import { PluginCommand } from "./PluginCommand";
import {
  materialSettingManager
} from "../views/WorkAreaComponents/MaterialConsumeStatisticsTool/MaterialSettingManager";
import {getInsMaterialInfoByDocInfosApi} from "../util/request/materialLibraryApi";

class MaterialSettingCommand extends PluginCommand{

  //零部件文档
  /** 获取当前零部件文档的具体信息
   * @param cb
   */
  async getCurrentDocInfo(cb) {
    if (this.toolQuery) {
      let res = await this.toolQuery.getCurrentPartDocInfo();
      if(res.code === 200){
        cb(res.data.document);
      }else {
        cb("")
      }
    }
  }
  /** 获取当前零部件文档的材料信息
   * @param pid docId零件文档id
   * @param cb
   */
  getCurrentPartMaterialInfo(pid,cb){
    materialSettingManager.getCurrentPartMaterialInfo(pid,tableData => {
        cb(tableData)
      })
  }
  /** 添加材料到当前零部件文档
   * @param info 材料信息
   * @param cb
   */
  addMaterialItemToCurrentPart(info,cb) {
    materialSettingManager.addMaterialItemToCurrentPart(info,tableData => {
      cb(tableData)
    })
  }
  /** 删除当前零部件文档挂接的材料信息
   * @param info 材料信息
   * @param cb
   */
  deleteMaterialItemToCurrentPart(info,cb) {
    materialSettingManager.deleteMaterialItemToCurrentPart(info,tableData => {
      cb(tableData)
    })
  }

  //2.装配文档

  /** 获取当前装配文件的实例树
   * @param cb
   */
  getInstanceTree(cb) {
    if(this.query) {
      this.query.getInstanceTree().then(res => {
        cb(res?.data?.instanceTree);
      });
    }
  }
  /** 获取当前装配文件全部实例信息（平铺所有实例信息)
   */
  async getInstancesProtoData() {
    if (this.toolQuery) {
      let res = await this.toolQuery.GetInstancesProtoData();
      if(res.code === 200){
        return res?.data?.protoData;
      }
    }
  }
  /** 1.根据实例Id获取实例信息
   * @param ids 实例id列表
   */
  async getInstanceInfo(ids) {
    if (this.query) {
      let res = await this.query.getInstancesByIds(ids);
      if(res.code === 200){
        return res?.data?.instances;
      }
    }
  }
  /** 2.根据实例Id获取实例信息
   * @param id 实例id
   */
  async getCurrentInstanceInfo(id) {
    if (this.toolQuery) {
      let res = await this.toolQuery.getInstanceProtoRefByInstanceId(id);
      if(res.code === 200){
        return res?.data?.instance;
      }
    }
  }
  /** 2.根据实例Id列表获取每个实例的材料信息
   * @param docInfoList 实例id列表
   * @param cb
   */
  getInsMaterialInfoByDocInfos(docInfoList,cb){
    materialSettingManager.getInsMaterialInfoByDocInfos(docInfoList,tableData => {
      cb(tableData)
    })
  }
  handlePickInstance(cb) {
    this.event.subscribeInstancePickEvent(res => (
        cb(res)
    ))
  }
}

const materialSettingCommand = new MaterialSettingCommand();
export { materialSettingCommand }
